#include <QDir>
#include <QTextStream>
#include <QDesktopWidget>

#include "QTremBinds.h"
#include "ui_QTremBinds.h"

QTremBinds::QTremBinds(QWidget *parent)
    : QWidget(parent), ui(new Ui::QTremBinds)
{
    ui->setupUi(this);

    // Add Widgets
    widConfiguration = new WidgetConfiguration(this);
    ui->gridLayMain->addWidget(widConfiguration, 1, 0);
    widConfiguration->hide();

    widBinds = new WidgetBinds(this);
    ui->gridLayMain->addWidget(widBinds, 1, 0);
    widBinds->hide();

    // Initialize values
    QDir dir;
    dir.cd(dir.homePath());
    if(dir.cd(".qtrembinds")){
        if(dir.exists("current_path.txt")){
            QFile file(dir.filePath("current_path.txt"));
            file.open(QIODevice::ReadOnly | QIODevice::Text);
            QTextStream in(&file);
            QString line = in.readLine();
            widConfiguration->setPath(line);
            file.close();
        }
    }

     // Initialize graphic configuration
    centerWidgetOnScreen(this);

    if(!widConfiguration->correctPath(widConfiguration->getPath())){
        ui->btnAddBind->hide();
    }


    // Connections
    connect(widConfiguration, SIGNAL(rejected()), this, SLOT(on_widConfiguration_rejected()));
    connect(widConfiguration, SIGNAL(accepted()), this, SLOT(on_widConfiguration_accepted()));

    connect(widBinds, SIGNAL(back()), this, SLOT(on_widBinds_back()));

}

QTremBinds::~QTremBinds()
{
    delete ui;
}


void QTremBinds::centerWidgetOnScreen (QWidget * widget) {
     QRect rect = QApplication::desktop()->availableGeometry();

     widget->move(rect.center() - widget->rect().center());
}


void QTremBinds::initializeBinds(){

    // I'm going to load the binds existing in the autogen.cfg

    // First get the path
    QDir dir;
    dir.cd(dir.homePath());
    dir.cd(".qtrembinds");
    QFile filePath(dir.filePath("current_path.txt"));
    filePath.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream in(&filePath);
    QString line = in.readLine();

    // go to the path
    dir.cd(line);
    dir.cd("base");
    filePath.close();

    // Open the files
    QFile fileAutogen(dir.filePath("autogen.cfg"));
    fileAutogen.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream reader(& fileAutogen);

    // Start to read the file
    bool start = false;
    while(!reader.atEnd() && !start){
        line = reader.readLine();
        if(line.contains("unbindall"))  // Look for the binds zone
            start = true;
    }
    line = reader.readLine();
    while((!reader.atEnd()) && line.contains("bind")){
        QStringList list = line.split(" ");
        QString element = list.at(1); // Take the key name
        Bind *bind = new Bind(element);
        list = line.split('"');
        QString actions = list.at(1);  // Take the actions list
        QStringList listActions = actions.split(";"); // Actions are separated by ;
        bind->setActions(listActions);
        binds_.addBind(bind);
        line = reader.readLine();
    }
    fileAutogen.close();
}

// SLOTS

void QTremBinds::on_btnAddBind_clicked()
{
    binds_.clearList();
    initializeBinds();
    widBinds->setBinds(binds_);

    widBinds->show();
    ui->widControls->hide();
}

void QTremBinds::on_btnConfig_clicked()
{
    widConfiguration->show();
    ui->widControls->hide();
}

void QTremBinds::on_widConfiguration_accepted(){
    ui->btnAddBind->show();
}

void QTremBinds::on_widConfiguration_rejected(){
    widConfiguration->hide();
    ui->widControls->show();
}

void QTremBinds::on_widBinds_back(){
    widBinds->hide();
    ui->widControls->show();
}
